package com.example.b_308.myapplication.view.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.view.title.TitleBar;

import java.io.Serializable;

/**
 * Created by xxtves on 2017/11/17.
 */

public class BaseActivity extends AppCompatActivity implements IBaseView{
    AlertDialog alertDialog;
    AlertDialog.Builder builder;
    LinearLayout ll;
    TitleBar titleBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        ll = (LinearLayout) findViewById(R.id.ll);
        titleBar = (TitleBar) findViewById(R.id.title_bar);
        builder =new AlertDialog.Builder(this);

    }
    public void setContentView(@LayoutRes int layoutResID) {
        ll.addView(LayoutInflater.from(this).inflate(layoutResID, null));
    }
    public TitleBar getTitleBar() {
        return titleBar;
    }

    public void tentTo(Class cla){
            startActivity(new Intent(this,cla));
    }

    public <T> void tendTo(Class cla,T t,String key){
        Intent intent =new Intent(this,cla);
        if(t instanceof String){
            intent.putExtra(key,(String)t);
        }
        if(t instanceof String[]){
            intent.putExtra(key,(String[])t);
        }
        if(t instanceof Long){
            intent.putExtra(key,(Long)t);
        }
        if(t instanceof Double){
            intent.putExtra(key,(Double)t);
        }
        if(t instanceof Integer){
            intent.putExtra(key,(Integer)t);
        }
        if(t instanceof Serializable){
            intent.putExtra(key,(Serializable)t);
        }

    }

    public <T> void tentForResoult(Class cla,int requestCode){
        Intent intent =new Intent(this,cla);
        startActivityForResult(intent,requestCode);
    }
    public <T extends View> T findId(int resouceId){
        return (T)findViewById(resouceId);
    }

    public void onFail(String message){
        builder.setTitle("提示").setMessage(message).setPositiveButton("确定",null);
        alertDialog =builder.create();
        alertDialog.show();
    }
}

